define(function(require, exports) {
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var popwin = require('@base/dialog').base;
	var util = require('util');

	// 添加客户账号popwin
	var SelectCustomerPopwin = popwin.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'title': LANG('选择联系人'),
				'width': 900,
				'height': 600,
				'showSub': true,
				'class': 'P-messageManageEditPopwin',
				'noFilter': false,
				// 配置getData获取的数据是否为ids
				'selectIds': false,
				'view_model': {

				}
			});

			this.$data = {};
			this.$selected = [];
			this.$ids = [];

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			self.Super('afterBuild',arguments);
			var el = self.getContainer('body');
			el.addClass('uk-form');
			var c = this.getConfig();
			el.append($([
				'<div class="M-tagLabelsWrapper">',
					'<div pub-mod="@base/taglabels.listType" pub-name="type"></div>',
					'<div pub-mod="@base/taglabels.listType" pub-name="categoryId"></div>',
				'</div>',
				'<div pub-name="grid" pub-mod="admin/messageManage/grid/grid.customerGrid"></div>'
			].join('')));

			if(c.noFilter)
			{
				el.find('.M-tagLabelsWrapper').hide();
			}

			var config = {
				'type':{
					'all':true,
					'collapse': true,
					'all_label':LANG('所有'),
					'title': LANG('类型：'),
					'data': [null, LANG('代理'), LANG('直客')]
				},
				'categoryId':{
					'all':true,
					'collapse': true,
					'all_label':LANG('所有'),
					'title': LANG('版本：'),
					'data': [null, LANG('有产品版'), LANG('无产品版')]
				},
				'grid': util.extend(this.getConfig('grid') || {}, {"param": {"Depth": 1}})
			};

			self.buildFromTemplate(config);
		},
		afterBuildTemplate: function(mod, data){
			var user = pubjs.getUser();

			if(this.getConfig('showSub'))
			{
				var grid = this.$.grid;
				var filter = grid.getContainer("filter");

				var showSub = $('<label><input type="checkbox"/>'+ LANG('显示从属子帐号') +'</label>');
				showSub.appendTo(filter);
				this.uiBind( showSub, 'click', 'eventShowSubClick' );

				if(user.current.level > 1){
					mod[0].hide();	// 隐藏过滤帐号级别功能
					filter.hide();	// 隐藏附加从属子帐号功能
				}
			}
		},
		setParam: function (param) {
			this.$.grid.setParam(param);
			return this;
		},
		reload: function()
		{
			this.$.grid.reload();
		},
		onListTypeChange: function(ev){
			var grid = this.$.grid;
			switch(ev.name){
				case 'type':// 类型：1代理 2直客
					this.$Type = ev.param.type;
				break;
				case 'categoryId':// 版本：1有产品版，2无产品版
					this.$CategoryId = ev.param.type;
				break;
			}
			grid.setParam({
				'Type':this.$Type,
				'CategoryId':this.$CategoryId
			});
			grid.load();
			return false;
		},
		// 显示从属子帐号
		eventShowSubClick: function(evt, elm){
			var grid = this.$.grid;
			var isChecked = $(elm).find('input').prop('checked');

			var Depth = isChecked ? 0 : 1;
			grid.setParam({
				'Depth': Depth
			});
			grid.load();
		},
		onOk: function(){
			this.hide();
			this.fire('selectCustomer', this.getData());
			return false;
		},
		onChangeSelect: function(ev) {
			var data = this.$data;
			util.each(ev.param.data, function(item, idx){
				data[item.UserId] = {
					'_id': item._id,
					'UserId': item.UserId,
					'Name': item.Name
				};
			});
			return false;
		},
		setData: function(ids, data) {
			this.$.grid.setSelectRowIds(ids);
			if(data)
			{
				this.$data = data;
			}

			return this;
		},
		getData: function(){
			var result = [];
			var ids = this.$.grid.getSelectRowIds();
			var c = this.getConfig();
			if (c.selectIds)
			{
				return ids;
			}
			else
			{
				var data = this.$data;
				util.each(ids, function(id, idx){
					if(data[id]){
						result.push(data[id]);
					}
				});
				return result;
			}
		},
		reset: function(){
			this.$data = {};
			this.$selected = [];
			this.$ids = [];
			// 设置好grid
			var grid = this.$.grid;
			grid.setSelectRowIds([]);
			// 重新加载数据
			grid.reload();
			return this;
		}
	});
	exports.selectCustomerPopwin = SelectCustomerPopwin;

	// 消息预览popwin
	var MessageCheckPopwin = popwin.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'title': LANG('公告详情'),
				'width': 1100,
				'height': 600,
				'class': 'uk-form P-messageManageCheckPopwin',
				'buttons': ['cancel', null],
				'view_model': {

				}
			});

			this.$data = {};
			this.Super('init', arguments);
		},
		afterBuild: function() {
			this.Super('afterBuild', arguments);
			// var c = this.getConfig();
			var el = this.getDOM('body');
			var cls = this.getConfig('class');
			el.addClass(cls);
			el.append($([
				'<div pub-mod="admin/messageManage/action/edit.check" pub-name="check" class="check"></div>'
			].join('')));

			var config = {
				'check': {
					'hasBackBtn': false
				}
			}

			this.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){

		},
		onOk: function() {
			this.hide();
			return false;
		},
		load: function(id){
			if(id){
				this.$.check.load(id);
			}
			return this;
		},
		reset:function(){
			this.$.check.reset();
			return this;
		}
	});
	exports.messageCheckPopwin = MessageCheckPopwin;
	
	var AptitudeCustomerPopwin = SelectCustomerPopwin.extend({
		afterBuildTemplate: function()
		{
			var grid = this.$.grid;
			var filter = grid.getContainer("filter");

			this.$showSelectEl = $('<label><input type="checkbox"/>'+ LANG('显示已选') +'</label>');
			this.$showSelectEl.appendTo(filter);
			this.uiBind( this.$showSelectEl, 'click', 'eventShowSelect' );

		},
		eventShowSelect: function(ev)
		{
			var elm = ev.target || ev.srcElement;
			if (elm.tagName === 'LABEL') {
				return;
			}

			var grid = this.$.grid;
			var isChecked = $(elm).prop('checked');

			var ids = grid.getSelectRowIds();
			
			ids = isChecked ? ids : null;
			
			var isEmpty = false;
			if (isChecked)
			{
				if (!ids || !ids.length)
				{
					isEmpty = true;
				}
			}
			
			// isEmpty表示，勾选了显示已选，但是ids为空，直接置空表单
			if (isEmpty)
			{
				grid.setData([]);
			}
			else
			{
				grid.setParam({
					'Ids': ids
				});
				grid.load();
			}
		},
		reset: function()
		{
			this.Super('reset', arguments);
			this.$showSelectEl.find('input').prop('checked', false);
			this.$.grid.setParam({'Ids': null});
			return this;
		}
	});
	exports.aptitudeCustomerPopwin  = AptitudeCustomerPopwin;

});